package com.usian.wemedia.controller;

import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.dtos.WmNewsDto;
import com.usian.model.media.dtos.WmNewsPageReqDto;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.pojos.WmNewsVo;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.WmNewsControllerApi;
import com.usian.wemedia.service.WmNewsService;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
public class WmNewsController implements WmNewsControllerApi {

    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmUserService wmUserService;


    /**
     * 文章列表查询
     *
     * @param dto
     * @return
     */
    @Override
    @RequestMapping("/list")
    public ResponseResult findPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findPage(dto);
    }


    /**
     * 文章发布
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/submit")
    public ResponseResult submitNews(@RequestBody  WmNewsDto dto) {
        if(dto.getStatus()== 1){
            //提交文章
            return wmNewsService.saveNews(dto, (short) 1);
        }else{
            //保存草稿
            return wmNewsService.saveNews(dto, (short) 0);
        }

    }

    /**
     * 修改文章 根据id查询返回
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findByNewId(@PathVariable Integer id) {
        return wmNewsService.findByNewId(id);
    }

    /**
     * 根据文章id删除文章
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/del_news/{id}")
    public ResponseResult deleteByNewId(@PathVariable Integer id) {
        return wmNewsService.deleteByNewId(id);
    }

    /**
     * 文章 上下架
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody  WmNewsDto dto) {
        return wmNewsService.downOrUp(dto);
    }

    /**
     * admin服务远程调用根据文章id查询文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/ones/{id}")
    public WmNews findByNewsId(@PathVariable Integer id) {
        WmNews wmNews = wmNewsService.getById(id);
        return wmNews;
    }

    /**
     * admin服务远程调用更新文章的审核状态
     *
     * @param wmNews
     * @return
     */
    @Override
    @PostMapping("/updateNewStatus")
    public ResponseResult updateNewStatus(@RequestBody WmNews wmNews) {
        return wmNewsService.updateNewStatus(wmNews);
    }

    /**
     * admin远程调用根据用户id查询用户对象
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/findByUserId/{id}")
    public WmUser findByUserId(@PathVariable("id") Integer userId) {
        return wmUserService.findByUserId(userId);
    }

    /**
     *  admin 远程调用 查询审核通过待发布的文章列表
     *
     * @return
     */
    @Override
    @PostMapping("/findNews")
    public List<Integer> findNews() {
        return wmNewsService.findNews();
    }

    /**
     *  admin 远程调用  查询人工审核列表分页查询
     *
     * @param dto
     * @return
     */
    @Override
    @PostMapping("/findList")
    public ResponseResult findList(@RequestBody NewsAuthDto dto) {
        return wmNewsService.findList(dto);
    }

    /**
     * admin 远程调用 查看人工审核文章详情
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findNewsById/{id}")
    public WmNewsVo findNewsById(@PathVariable Integer id) {
        return wmNewsService.findNewsById(id);
    }
}
